package pl.edu.pw.MidEmu.UI.Controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;

import pl.edu.pw.MidEmu.Base.Controllers.BaseController;
import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.UI.Views.AddSensorView;

/**
 * Controller for adding new sensor
 * @author Mateusz Starodab
 *
 */
public class AddSensorController extends BaseController<AddSensorView> implements ActionListener
{

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() instanceof JRadioButton)
		{
			JRadioButton button = (JRadioButton) e.getSource();
			if (button.getName() == Consts.AddSensor_RadioEndless)
			{
				getView().disableIterationBox(true);
			}
			else if (button.getName() == Consts.AddSensor_RadioIterational)
			{
				getView().disableIterationBox(false);
			}
			else if (button.getName() == Consts.AddSensor_RadioContinous)
			{
				getView().disableIntervalBox(true);
			}
			else if (button.getName() == Consts.AddSensor_RadioInterval)
			{
				getView().disableIntervalBox(false);
			}
			else if (button.getName() == Consts.AddSensor_RadioConstant)
			{
				getView().disableLoopBox(false);
				getView().enableExternalFile(false);
			}
			else if (button.getName() == Consts.AddSensor_RadioExternal)
			{
				getView().disableLoopBox(false);
				getView().enableExternalFile(true);
			}
			else if (button.getName() == Consts.AddSensor_RadioIncrement)
			{
				getView().disableLoopBox(true);
				getView().enableExternalFile(false);
			}
			else if (button.getName() == Consts.AddSensor_RadioDecrement)
			{
				getView().disableLoopBox(true);
				getView().enableExternalFile(false);
			}
			else if (button.getName() == Consts.AddSensor_RadioRandom)
			{
				getView().disableLoopBox(true);
				getView().enableExternalFile(false);
			}
		}
		else if (e.getSource() instanceof JCheckBox)
		{
			JCheckBox box = (JCheckBox) e.getSource();
			if (box.getName() == Consts.AddSensor_Range)
			{
				getView().disableRangeBox(box.getModel().isSelected());
			}
		}
		else if (e.getSource() instanceof JButton)
		{
			JButton button = (JButton) e.getSource();
			if (button.getName() == Consts.AddSensor_OK)
			{
				if (getView().validate())
					getView().setSensor();
				else
					getView().reportValidationErrors();
			}
			if (button.getName() == Consts.AddSensor_BrowseButton)
			{
				getView().showFileDialog();
			}
		}
	}
}
